/**
 * @fileOverview 网格数据结构
 * @author goodzsq@gmail.com
 */

var cellAdd = function(grid, i, j, id) {
    if (!grid[i][j]) {
        grid[i][j] = [];
    }
    grid[i][j].push(id);
};

var cellRemove = function(grid, i, j, id) {
    if (grid[i] && grid[i][j]) {
        var at = _.indexOf(grid[i][j], id);
        if (at >= 0) {
            grid[i][j].splice(at, 1);
        }
    }
};

var BaseClass = gg.module('struct.List');
module.exports = gg.createClass({
    attributes: {
        i: {
            type: 'datatype.Integer',
            defaultValue: 10,
            setter: function(v) {
                this.defaultSetter('i', v);
                this.grid.length = v;
            }
        },
        j: {
            type: 'datatype.Integer',
            defaultValue: 5,
            setter: function(v) {
                this.defaultSetter('j', v);
                for (var i = 0, max = this.get('i'); i < max; i++) {
                    if (!this.grid[i]) {
                        this.grid[i] = [];
                    }
                    this.grid[i].length = v;
                }
            }
        }
    },
    methods: {
        grid: [],
        /**
         * @override
         * @param {String} id1, id2, id3 ...
         */
        killObject: function(/*,...*/) {
            for (var i = 0, max = arguments.length; i < max; i++) {
                var id = arguments[i];
                var obj = this.getObject(id);
                if (obj) {
                    cellRemove(this.grid, obj.i, obj.j, obj.id);
                }
            }
            BaseClass.prototype.killObject.apply(this, arguments);
        },
        /**
         * 移动对象
         * @param {String} id
         * @param {Number} i
         * @param {Number} j
         */
        moveObject: function(id, i, j) {
            var obj = this.getObject(id);
            if (!obj) {
                return false;
            }
            var a = obj.i, b = obj.j;
            obj.i = i;
            obj.j = j;
            cellRemove(this.grid, a, b, id);
            cellAdd(this.grid, i, j, id);
            this.trigger('onMoveObject', this, id, a, b, i, j);
        },
        /**
         * 获取单元格内对象id列表
         * @param {Number} i
         * @param {Number} j
         * @param {Array} object id list
         */
        getCell: function(i, j) {
            return this.grid[i][j];
        },
        /**
         * 获取单元格内的对象
         * @param {Number} i
         * @param {Number} j
         * @param {Number} at 默认值为0
         * @return {Object}
         */
        getCellObject: function(i, j, at) {
            var cell = this.grid[i][j];
            at = at || 0;
            if (cell && cell[at]) {
                return this.getObject(cell[0]);
            } else {
                return null;
            }
        }
    },
    events: {
        'onMoveObject': '移动单元格'
    }
}, BaseClass, []);

/** @fileend */

//unittest
UnitTest.add('struct.Grid模块测试',
        function() {
            var grid = gg.object({
                classType: 'struct.Grid'
            });
            grid.addObject('qq1');
            grid.addObject('qq2');
            grid.addObject('qq3');
            grid.moveObject('qq1', 1, 1);
            grid.moveObject('qq2', 1, 1);
            grid.moveObject('qq3', 1, 1);
            grid.killObject('qq2');
        }
);